using FytSoa.Application.Erp.Dto;
using FytSoa.Application.Erp.Param;
using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;
[ApiExplorerSettings(GroupName = "v9")]
public class ErpFinanceStatisticsService : IApplicationService 
{
    private readonly SugarRepository<ErpFinanceRefund> _refundRepository;
    private readonly SugarRepository<ErpFinancePayment> _paymentRepository;
    private readonly SugarRepository<ErpFinanceReimbursement> _reimbursementRepository;
    public ErpFinanceStatisticsService(SugarRepository<ErpFinanceRefund> refundRepository
    ,SugarRepository<ErpFinancePayment> paymentRepository
    ,SugarRepository<ErpFinanceReimbursement> reimbursementRepository)
    {
        _refundRepository = refundRepository;
        _paymentRepository = paymentRepository;
        _reimbursementRepository = reimbursementRepository;
    }

    /// <summary>
    /// 根据年份查询12个月的付款以及回款
    /// </summary>
    /// <returns></returns>
    public async Task<FinanceStatisticsYearDto> GetYearAsync(FinanceStatisticsYearParam param)
    {
        var paymentList = await _paymentRepository.Context.Queryable<ErpFinancePayment>()
            .Where(m=>SqlFunc.DateIsSame(m.CreateTime,DateTime.Now,DateType.Year))
            .WhereIF(param.SupplierId!=0,m=>m.SupplierId==param.SupplierId)
            .WhereIF(!string.IsNullOrEmpty(param.ModeId),m=>m.ModeId==long.Parse(param.ModeId))
            .Select(m => new
            {
                m.Amount,
                Time = m.CreateTime.Date
            })
            .MergeTable()
            .GroupBy(m => new {m.Time})
            .Select(m => new
            {
                m.Time,
                Amount = SqlFunc.AggregateSum(m.Amount)
            })
            .ToListAsync();
        
        var refundList = await _refundRepository.Context.Queryable<ErpFinanceRefund>()
            .Where(m=>SqlFunc.DateIsSame(m.CreateTime,DateTime.Now,DateType.Year))
            .WhereIF(param.SupplierId!=0,m=>m.SupplierId==param.SupplierId)
            .WhereIF(!string.IsNullOrEmpty(param.ModeId),m=>m.ModeId==long.Parse(param.ModeId))
            .Select(m => new
            {
                m.Amount,
                Time = m.CreateTime.Date
            })
            .MergeTable()
            .GroupBy(m => new {m.Time})
            .Select(m => new
            {
                m.Time,
                Amount = SqlFunc.AggregateSum(m.Amount)
            })
            .ToListAsync();
        
        var reimbursementList = await _reimbursementRepository.Context.Queryable<ErpFinanceReimbursement>()
            .Where(m=>SqlFunc.DateIsSame(m.CreateTime,DateTime.Now,DateType.Year))
            .WhereIF(!string.IsNullOrEmpty(param.ModeId),m=>m.ModeId==long.Parse(param.ModeId))
            .Select(m => new
            {
                m.Amount,
                Time = m.CreateTime.Date
            })
            .MergeTable()
            .GroupBy(m => new {m.Time})
            .Select(m => new
            {
                m.Time,
                Amount = SqlFunc.AggregateSum(m.Amount)
            })
            .ToListAsync();
        
        var month = new List<string>();
        var payment = new List<decimal>();
        var refund = new List<decimal>();
        var reimbursement = new List<decimal>();
        for (int i = 1; i < 13; i++)
        {
            month.Add(i+"月");
            var monthSum = paymentList.Where(m => m.Time.Month == i && m.Time.Year==param.Year).Sum(m=>m.Amount);
            payment.Add(monthSum);
            var refundSum=refundList.Where(m => m.Time.Month == i && m.Time.Year==param.Year).Sum(m=>m.Amount);
            refund.Add(refundSum);
            var reimbursementSum=reimbursementList.Where(m => m.Time.Month == i && m.Time.Year==param.Year).Sum(m=>m.Amount);
            reimbursement.Add(reimbursementSum);
        }
        return new FinanceStatisticsYearDto(){Month = month,Payment = payment,Refund = refund,Reimbursement=reimbursement};
    }
}